var totalSessionTime = 0;
var exitPageStatus;
//var result = doLMSInitialize();
var lastlocation = 0;
var brAlreadyComputeTime = false;
var sco_startDate = 0;

var autoStop = false;

var exitFromInner = false;

function loadPage() {
  var dragStatus = window.parent.courseFrameFn().isAllowDrag;

  if (dragStatus) {
    enableDrag = true;

    PROGRESS_TIMES = 1.1;
  }

  /*   enableDrag = true;  */

  exitPageStatus = false;

  var stopStatus = window.parent.courseFrameFn().isPreventHangUp;

  if (stopStatus) autoStop = true;

  VIDEO_PATH = window.parent.courseFrameFn().LMSGetValue("cmi.core.videoUr1");

  lastlocation = doGetLaunch();

  if (lastlocation == "") lastlocation = 0;
}
function closeWin() {
  var ua = navigator.userAgent;
  var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false;
  if (ie) {
    var IEversion = parseFloat(
      ua.substring(
        ua.indexOf("MSIE ") + 5,
        ua.indexOf(";", ua.indexOf("MSIE "))
      )
    );
    if (IEversion < 5.5) {
      var str =
        '<object id=noTipClose classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">';
      str += '<param name="Command" value="Close"></object>';
      document.body.insertAdjacentHTML("beforeEnd", str);
      document.all.noTipClose.Click();
    } else {
      window.opener = null;
      window.close();
    }
  } else {
    window.close();
  }
}
function windowclose() {
  var browserName = navigator.appName;
  if (browserName == "Netscape") {
    window.open("", "_self", "");
    window.close();
  } else {
    if (browserName == "Microsoft Internet Explorer") {
      window.opener = "whocares";
      window.opener = null;
      window.open("", "_top");
      window.close();
    }
  }
  window.close();
  parent.window.close();
  parent.parent.window.close();
  parent.parent.parent.window.close();
}
function unloadPage(status, location) {
  //alert(location);
  doQuit(status, location);
  window.setTimeout(windowclose, 500);
}
function doSetLaunch(location) {
  window.parent
    .courseFrameFn()
    .LMSSetValue("cmi.core.lesson_location", location);

  /*   if(location>totalDuration-3){
   	doLMSSetValue("cmi.core.lesson_status", "completed" );
  } */
}

//myc修改
function reSetSessionTime() {
  totalSessionTime = 0;
}

//myc修改结束
function sco_startTimer() {
  sco_startDate = new Date().getTime();
}
function sco_computeTime() {
  var elapsedSeconds = 0;

  if (sco_startDate != 0) {
    var sco_currentDate = new Date().getTime();

    CURRENT_DATE_TIME = timestampToDatetime(sco_currentDate);

    var elapsedSeconds = (sco_currentDate - sco_startDate) / 1000;
  }
  if (brAlreadyComputeTime == false) {
    totalSessionTime = totalSessionTime + elapsedSeconds;
  }
}

function doGetLaunch() {
  result = window.parent
    .courseFrameFn()
    .LMSGetValue("cmi.core.lesson_location");
  /* var result = 0; */
  return result;
}
function makeTimeToSuit(time) {
  var temp1 = Math.floor(time);
  var hou1 = Math.floor(temp1 / 3600);
  var temp2 = Math.floor(temp1 % 3600);
  var min1 = Math.floor(temp2 / 60);
  var sec1 = Math.floor(temp2 % 60);
  if (hou1 > 0) {
    var longTime = hou1 + "小时" + min1 + "分钟" + sec1 + "秒";
  } else {
    var longTime = min1 + "分钟" + sec1 + "秒";
  }
  return longTime;
}
function doQuit(status, location) {
  doSetLaunch(location);
  //doLMSSetValue( "cmi.core.exit", "logout" );

  if (!videoPause) sco_computeTime();
  //alert(status)
  setSessionTime();
  var lesson_status = doLMSGetValue("cmi.core.lesson_status");
  if (lesson_status != "completed")
    result = window.parent
      .courseFrameFn()
      .LMSSetValue("cmi.core.lesson_status", status);
  /*    var result;
   result = doLMSFinish(); */
}
function setSessionTime() {
  if (sco_startDate != 0) {
    var formattedTime = convertTotalSeconds(totalSessionTime);
  } else {
    formattedTime = "00:00:00.0";
  }

  // doLMSSetValue( "cmi.core.session_time", formattedTime );
}
function EndPlay() {
  if (exitFromInner) return;
  else doQuit("incomplete", currentime);
}

window.saveForExit = saveForExit;
function saveForExit() {
  doQuit("incomplete", currentime);
}

//window.onbeforeunload=EndPlay;  //
//window.onunload=EndPlay;  //
/*******************************************************************************
 ** this function will convert seconds into hours, minutes, and seconds in
 ** CMITimespan type format - HHHH:MM:SS.SS (Hours has a max of 4 digits &
 ** Min of 2 digits
 *******************************************************************************/
function convertTotalSeconds(ts) {
  var sec = ts % 60;
  ts -= sec;
  var tmp = ts % 3600; //# of seconds in the total # of minutes
  ts -= tmp; //# of seconds in the total # of hours
  // convert seconds to conform to CMITimespan type (e.g. SS.00)
  sec = Math.round(sec * 100) / 100;
  var strSec = new String(sec);
  var strWholeSec = strSec;
  var strFractionSec = "";
  if (strSec.indexOf(".") != -1) {
    strWholeSec = strSec.substring(0, strSec.indexOf("."));
    strFractionSec = strSec.substring(strSec.indexOf(".") + 1, strSec.length);
  }
  if (strWholeSec.length < 2) {
    strWholeSec = "0" + strWholeSec;
  }
  strSec = strWholeSec;
  if (strFractionSec.length) {
    strSec = strSec + "." + strFractionSec;
  }
  if (ts % 3600 != 0) var hour = 0;
  else var hour = ts / 3600;
  if (tmp % 60 != 0) var min = 0;
  else var min = tmp / 60;
  if (new String(hour).length < 2) hour = "0" + hour;
  if (new String(min).length < 2) min = "0" + min;
  var rtnVal;
  if (isNaN(strSec)) rtnVal = hour + ":" + min + ":" + "00.50";
  else rtnVal = hour + ":" + min + ":" + strSec;
  return rtnVal;
}

function timestampToDatetime(timestamp) {
  const dateObj = new Date(timestamp);

  const year = dateObj.getFullYear();
  const month = dateObj.getMonth() + 1;
  const day = dateObj.getDate();
  const hours = dateObj.getHours();
  const minutes = dateObj.getMinutes();
  const seconds = dateObj.getSeconds();

  return (
    year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds
  );
}

loadPage();
